package com.bytz.modules.cms.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bytz.common.api.vo.Result;
import com.bytz.modules.cms.common.constants.AdminAuthConstants;
import com.bytz.modules.cms.common.util.ExcelUtil;
import com.bytz.modules.cms.product.entity.ProductEcLicenceUser;
import com.bytz.modules.cms.product.model.ProductEcLicenceUserModel;
import com.bytz.modules.cms.product.service.IProductEcLicenceUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@Api(tags = "许可证清单")
@RequestMapping("/productEcLicenceUser")
@PreAuthorize("hasAuthority('" + AdminAuthConstants.ProductEcLicenceUser.EDIT + "')")
public class AdminProductEcLicenceUserController {
    @Autowired
    private IProductEcLicenceUserService productEcLicenceUserService;

    @ApiOperation(value = "分页查询", notes = "分页查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<IPage<ProductEcLicenceUserModel>> page(ProductEcLicenceUserModel productEcLicenceUserModel,
                                              @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                              @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                              HttpServletRequest req) throws Exception {

        return Result.ok(productEcLicenceUserService.queryPage(new Page<>(pageNo, pageSize), productEcLicenceUserModel, req.getParameterMap()));
    }

    @ApiOperation(value = "根据ID删除许可证清单", notes = "根据ID删除许可证清单")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Result<Boolean> delete(@RequestParam String id) {
        return Result.ok(productEcLicenceUserService.removeById(id));
    }

    @ApiOperation(value = "获取导入模板", notes = "获取导入模板")
    @RequestMapping(value = "/template", method = RequestMethod.POST)
    public void template(HttpServletResponse response) throws Exception {
        ExcelUtil.exportTemp("许可证清单Excel模板", "Sheet1", ProductEcLicenceUser.class, response);
    }

    @ApiOperation(value = "导入许可证清单", notes = "导入许可证清单")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<List<String>> importExcel(HttpServletRequest request) {
        List<ProductEcLicenceUser> models = ExcelUtil.importGroup(request, ProductEcLicenceUser.class, null);
        List<String> list = productEcLicenceUserService.importExcel(models);
        return Result.ok(list.size()==0?"导入成功":"以上终端用户不存在，但仍已插入",list);
    }

    @ApiOperation(value = "导出excel", notes = "导出excel")
    @RequestMapping(value = "/exportXls", method = RequestMethod.POST)
    public void export(HttpServletResponse response) throws Exception {
        productEcLicenceUserService.export(response);
    }
}
